<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetHeatmapDataApi} from "@/api";
import 'echarts-wordcloud'
import type {Heatmap} from "@/interface";

const heatmap = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const heatmapData = ref<Heatmap>({
  data:[],
  floor_type_list:[],
  house_type_list:[]
})
const init = async () => {
  const {data: res} = await useGetHeatmapDataApi()
  heatmapData.value = res
  myChart.value = markRaw(echarts.init(heatmap.value));
  draw();
}
const draw = () => {
  const data = heatmapData.value.data.map((item:number[])=>[item[1], item[0], item[2] || '-'])
  option.value = {
    title:{
      text:'不同楼层与户型的数量分布热力图',
      left:'center'
    },
    tooltip: {
      position: 'top'
    },
    grid: {
      height: '50%',
      top: '10%'
    },
    xAxis: {
      type: 'category',
      data: heatmapData.value.house_type_list,
      splitArea: {
        show: true
      }
    },
    yAxis: {
      type: 'category',
      data: heatmapData.value.floor_type_list,
      splitArea: {
        show: true
      }
    },
    visualMap: {
      min: 0,
      max: 400,
      calculable: true,
      orient: 'horizontal',
      left: 'center',
      bottom: '15%'
    },
    series: [
      {
        name: 'Punch Card',
        type: 'heatmap',
        data: data,
        label: {
          show: true
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="heatmap" class="heatmap">
  </div>
</template>

<style scoped>
.heatmap {
  width: 100%;
  height: 100%;
}
</style>